package com.ls.blog.controller;

import static com.ls.blog.common.consts.Methods.*;

import com.ls.blog.dto.ResourceDTO;
import com.ls.blog.entity.Resource;
import com.ls.blog.service.ResourceService;
import com.ls.blog.utils.R;
import com.ls.blog.utils.Result;
import com.ls.blog.vo.ResourceV0;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lishun
 * @since 2022-01-17
 */
@RestController
@RequestMapping("/resource")
@Api(tags = "资源模块")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/admin/importSwagger")
    @ApiOperation(value = "后台导入swagger",httpMethod = GET)
    public Result<?> importSwagger(){
        resourceService.importSwagger();
        return R.ok();
    }

    @GetMapping("/admin/listOpt")
    @ApiOperation(value = "后台资源选项",httpMethod = GET)
    public Result<List<ResourceDTO>> listOpt(){
        return R.ok(resourceService.listOpt());
    }

    @GetMapping("/admin/listByRoleId/{id}")
    @ApiOperation(value = "根据角色id查找资源id列表",httpMethod = GET)
    public Result<List<Integer>> listByRoleId(@PathVariable("id")Integer id){
        return R.ok(resourceService.listByRoleId(id));
    }

    @PutMapping("/admin/updateRoleResource")
    @ApiOperation(value = "修改角色资源列表",httpMethod = PUT)
    public Result<?> updateRoleResource (@RequestBody ResourceV0 resourceV0){
        resourceService.updateRoleResource(resourceV0);
        return R.ok();
    }


    @GetMapping("/admin/list")
    @ApiOperation(value = "后台资源列表",httpMethod = GET)
    public Result<List<ResourceDTO>> list(){
        return R.ok( resourceService.listResourceShow());
    }

    @PutMapping("/admin/update")
    @ApiOperation(value = "修改匿名状态",httpMethod = PUT)
    public Result<?> updateIsAnonymous(@RequestBody ResourceV0 resourceV0){
            resourceService.updateIsAnonymous(resourceV0);
            return R.ok();
        }

}
